package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.InstitutionMedication;
import com.wty.cmims.pojo.InstitutionMedication;
import com.wty.cmims.pojo.Medication;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.InstitutionMedicationService;
import com.wty.cmims.mapper.InstitutionMedicationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【institution_medication】的数据库操作Service实现
* @createDate 2024-12-10 23:14:44
*/
@Service
public class InstitutionMedicationServiceImpl extends ServiceImpl<InstitutionMedicationMapper, InstitutionMedication>
    implements InstitutionMedicationService{
    @Autowired
    private InstitutionMedicationMapper institutionMedicationMapper;
    @Override
    public ResponseResult<Map<String, Object>> findIMByInstitutionId(Map<String,Object> paramsMap) {
        // 分页
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);

        List<Map<String, Object>> institutionMedications = institutionMedicationMapper.findIMByInstitutionId(paramsMap);
        Integer total = institutionMedicationMapper.findIMNum(paramsMap);
        if (institutionMedications != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗机构——药品信息成功");
            responseResult.setDatas(institutionMedications);
            responseResult.setTotal(total);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗机构——药品信息失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> updateIMStock(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<InstitutionMedication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InstitutionMedication::getInstitutionId, paramsMap.get("institution_id"))
                .eq(InstitutionMedication::getMedicationId, paramsMap.get("medication_id"))
                .set(InstitutionMedication::getStock, paramsMap.get("stock"));
        boolean update = this.update(wrapper);
        if (update) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("库存修改成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("库存修改失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> addIM(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        InstitutionMedication institutionMedication = new InstitutionMedication();
        institutionMedication.setInstitutionId(Integer.parseInt((String) paramsMap.get("institution_id")));
        institutionMedication.setMedicationId((Integer) paramsMap.get("medication_id"));
        institutionMedication.setStock((Integer) paramsMap.get("stock"));
        boolean save = this.save(institutionMedication);
        if (save) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("药品添加成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("药品添加失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> deleteIM(Integer institutionId, Integer medicationId) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<InstitutionMedication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InstitutionMedication::getInstitutionId,institutionId);
        wrapper.eq(InstitutionMedication::getMedicationId,medicationId);
        wrapper.set(InstitutionMedication::getIsDel,1);
        boolean delete = this.update(wrapper);
        if (delete) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("药品删除成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("药品删除失败");
        }
        return responseResult;
    }
}